package org.example.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @author mengll
 * @date 2020/3/9 19:47
 */
@Slf4j
public class DateUtils {
    /**
     * 日期时间字符常量,日期以<code>-</code>分割,时分秒以<code>:</code>分割
     * 格式为<code>yyyy-MM-dd HH:mm:ss</code>
     */
    public static final String FMT_DATE_TIME_DEFAULT = "yyyy-MM-dd HH:mm:ss";
    /**
     * 日期时间字符常量,日期以<code>/</code>分割,时分秒以<code>:</code>分割
     * 格式为<code>yyyy/MM/dd HH:mm:ss</code>
     */
    public static final String FMT_DATE_TIME_SLASH = "yyyy/MM/dd HH:mm:ss";
    /**
     * 日期时间字符常量,无分隔符,格式为<code>yyyyMMddHHmmss</code>
     */
    public static final String FMT_DATE_TIME_NOT_SEPARATION = "yyyyMMddHHmmss";
    /**
     * 日期字符常量,日期以<code>-</code>分割
     * 格式为<code>yyyy-MM-dd</code>
     */
    public static final String FMT_DATE_DEFAULT = "yyyy-MM-dd";
    /**
     * 日期字符常量,日期以<code>/</code>分割
     * 格式为<code>yyyy/MM/dd</code>
     */
    public static final String FMT_DATE_SLASH = "yyyy/MM/dd";
    /**
     * 时间字符常量,时间以<code>:</code>分割
     * 格式为<code>HH:mm</code>
     */
    public static final String FMT_TIME_DEFAULT = "HH:mm";


    public static String getDateTimeFormat(LocalDateTime date) {
        return new SimpleDateFormat(FMT_DATE_TIME_DEFAULT).format(date);
    }

    /**
     * 验证传入的字符串是否是日期类型,目前支持四种格式
     * 如果可以转换日期则返回<code>yyyy-MM-dd</code>格式
     * 否则返回null
     *
     * @param dateStr
     * @return
     */
    public static String validateDateFormat(String dateStr) {
        String[] strArr = {FMT_DATE_TIME_DEFAULT, FMT_DATE_TIME_SLASH, FMT_DATE_DEFAULT, FMT_DATE_SLASH};
        boolean dateFlag = false;
        String dStr = null;
        Date date;
        for (String strFormat : strArr) {
            try {
                SimpleDateFormat format = new SimpleDateFormat(strFormat);
                date = format.parse(dateStr);
                SimpleDateFormat format1 = new SimpleDateFormat(FMT_DATE_DEFAULT);
                dStr = format1.format(date);
                dateFlag = true;
            } catch (ParseException ex) {
            }
            if (dateFlag) {
                return dStr;
            }
        }
        return null;
    }

    /**
     * 判断两个时间相隔的天数
     *
     * @param time1
     * @param time2
     * @return
     */
    public static Integer beApartTime(String time1, String time2) {
        SimpleDateFormat df = new SimpleDateFormat(FMT_DATE_DEFAULT);
        Date firstDate;
        Date secondDate;
        int nDay = 0;
        try {
            firstDate = df.parse(time1);
            secondDate = df.parse(time2);
            nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));
        } catch (Exception e) {
            log.error("", e);
        }
        return nDay;
    }

    /**
     * .
     * 获取当前时间的前n个月或者后n个月
     *
     * @param format 返回格式
     * @param n      月数
     * @param flag   1 表示当前时间前,2表示当前时间后
     * @return String
     */
    public static String getBeforeOfMonth(String format, int n, int flag) {

        if (StringUtils.isEmpty(format)) {
            format = FMT_DATE_TIME_DEFAULT;
        }

        SimpleDateFormat df = new SimpleDateFormat(format);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DATE, 1);

        if (1 == flag) {
            calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - n);
        } else {
            calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + n);
        }
        return df.format(calendar.getTime());
    }

    /**
     * 根据传入的日期和日期格式转成相对应的字符串
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String format(Date date, String pattern) {
        SimpleDateFormat sf = new SimpleDateFormat(pattern);
        return sf.format(date);
    }

    /**
     * 获取系统当前日期时间(格式自定)
     * 返回日期的格式 默认为 yyyy-MM-dd HH:mm:ss
     *
     * @param format
     * @return
     */
    public static String getFormatDate(String format) {
        if (StringUtils.isEmpty(format)) {
            format = FMT_DATE_TIME_DEFAULT;
        }
        Date d = new Date();
        DateFormat df = new SimpleDateFormat(format);

        return df.format(d);
    }

    /**
     * .
     * 获取当前日期的前n天或后n天的时间
     *
     * @param i      天数
     * @param format 返回日期的格式 默认为 yyyy-MM-dd HH:mm:ss
     * @return String
     */
    @SuppressWarnings("deprecation")
    public static String getOtherDayTime(int i, String format) {
        if (StringUtils.isEmpty(format)) {
            format = FMT_DATE_TIME_DEFAULT;
        }
        Date nowDate = new Date();
        DateFormat df = new SimpleDateFormat(format);
        nowDate.setDate(nowDate.getDate() + i);
        return df.format(nowDate);
    }

    /**
     * 验证当前月份是否在当前X个月以内
     *
     * @param month 月
     * @param scope X
     * @return boolean
     */
    public static boolean checkMonth(int month, int scope) {
        Date date = new Date();
        DateFormat df = new SimpleDateFormat("MM");
        int nowmonth = Integer.parseInt(df.format(date));
        int i = scope - 1;
        if (month < 1 || month > 12) {
            return false;
        } else {
            if (nowmonth > i) {
                return !(month > nowmonth || month < nowmonth - i);
            } else {
                return !(month < nowmonth || (month > nowmonth + (12 - scope) && month <= 12));
            }
        }
    }

    /**
     * 获取当前日期前N个月份列表
     *
     * @param scope N
     * @return List
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public List getMonthlist(int scope) {
        List list = new ArrayList();
        for (int i = 0; i < scope; i++) {
            HashMap map = new HashMap();
            String startdate = getBeforeOfMonth("yyyyMMdd", i, 1);
            String enddate = getBeforeOfMonth("yyyyMMdd", i - 1, 1);
            String name = startdate.substring(0, 4) + "年" + startdate.substring(4, 6);
            map.put("startdate", startdate);
            map.put("enddate", enddate);
            map.put("name", name);
            list.add(map);
        }

        return list;
    }

    /**
     * .
     * 获取当前月份
     *
     * @return String
     */
    public static String getMonth() {
        Date date = new Date();
        DateFormat df = new SimpleDateFormat("MM");

        return df.format(date);
    }

    /**
     * .
     * 获取当前小时
     *
     * @return String
     */
    public static String getHour() {
        Date date = new Date();
        DateFormat df = new SimpleDateFormat("HH");

        return df.format(date);

    }

    /**
     * .
     * 获取当前年份
     *
     * @return String
     */
    public static String getYear() {
        Date date = new Date();
        DateFormat df = new SimpleDateFormat("yyyy");

        return df.format(date);
    }

    /**
     * .
     *
     * @return String
     */
    public static String getMonth(Date date) {
        DateFormat df = new SimpleDateFormat("MM");
        return df.format(date);
    }

    /**
     * .
     *
     * @return String
     */
    public static String getHour(Date date) {
        DateFormat df = new SimpleDateFormat("HH");
        return df.format(date);
    }

    /**
     * .
     *
     * @return String
     */
    public static String getYear(Date date) {
        DateFormat df = new SimpleDateFormat("yyyy");
        return df.format(date);
    }

    /**
     * .
     * 获取当前日期
     *
     * @return String
     */
    public static String getDate() {
        Date date = new Date();
        DateFormat df = new SimpleDateFormat("dd");

        return df.format(date);
    }

    /**
     * .
     * 判断指定的日期是否已过期
     *
     * @param checkedDate  日期
     * @param milliseconds 日期
     * @return boolean
     */
    public static boolean isOverdue(Date checkedDate, long milliseconds) {
        Calendar cd = Calendar.getInstance();
        Date now = cd.getTime();
        // 时间往前移多少毫秒
        now.setTime(now.getTime() - milliseconds);

        return checkedDate.before(now);
    }

    /**
     * .
     * 将指定字符串格式的时间串转化为需要的格式时间串
     *
     * @param numDate   时间
     * @param inFormat  输入格式
     * @param outFormat 输出格式
     * @return String
     */
    public static String changeNumDateToDate(String numDate, String inFormat, String outFormat) {
        String returnDate = null;
        Date date;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(inFormat);
            date = sdf.parse(numDate);
            sdf = new SimpleDateFormat(outFormat);
            returnDate = sdf.format(date);
        } catch (Exception e) {
            log.debug("日期转换失败 ", e);
        }
        return returnDate;
    }

    /**
     * .
     * 获取每个月的第一天
     *
     * @param nowDate   时间
     * @param inFormat  输入格式
     * @param outFormat 输出格式
     * @return String
     */
    public static String getFistDay(String nowDate, String inFormat, String outFormat) {

        String returnDate = null;

        Date date;
        SimpleDateFormat sdf = new SimpleDateFormat(inFormat);
        try {
            date = sdf.parse(nowDate);
            Calendar cl = Calendar.getInstance();
            cl.setTime(date);
            cl.set(Calendar.DAY_OF_MONTH, 1);
            date = cl.getTime();
            sdf = new SimpleDateFormat(outFormat);
            returnDate = sdf.format(date);
        } catch (ParseException e) {
            log.debug("获取该月第一天失败", e);
        }
        return returnDate;
    }

    /**
     * .
     * 获取后一个月的第一天
     *
     * @param nowDate   时间
     * @param inFormat  输入格式
     * @param outFormat 输出格式
     * @return String
     */
    public static String getNextMonthFistDay(String nowDate, String inFormat, String outFormat) {
        String returnDate = null;
        Date date;
        SimpleDateFormat sdf = new SimpleDateFormat(inFormat);
        try {
            date = sdf.parse(nowDate);
            Calendar cl = Calendar.getInstance();
            cl.setTime(date);
            cl.set(Calendar.MONTH, cl.get(Calendar.MONTH) + 1);
            cl.set(Calendar.DAY_OF_MONTH, 1);
            date = cl.getTime();
            sdf = new SimpleDateFormat(outFormat);
            returnDate = sdf.format(date);
        } catch (ParseException e) {
            log.debug("获取该月第一天失败", e);
        }
        return returnDate;
    }

    /**
     * .
     * 获取后一个月的第一天
     *
     * @param fmt 格式
     * @return String
     */
    public static String getNextMonthFirstDay(String fmt) {
        String returnDate;
        Date date;
        Calendar cl = Calendar.getInstance();
        cl.set(Calendar.MONTH, cl.get(Calendar.MONTH) + 1);
        cl.set(Calendar.DAY_OF_MONTH, 1);
        date = cl.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(fmt);
        returnDate = sdf.format(date);
        return returnDate;
    }

    /**
     * .
     * 获取当月的第一天
     *
     * @param fmt 格式
     * @return String
     */
    public static String getCurrentMonthFirstDay(String fmt) {
        String returnDate;
        Date date;
        Calendar cl = Calendar.getInstance();
        cl.set(Calendar.MONTH, cl.get(Calendar.MONTH));
        cl.set(Calendar.DAY_OF_MONTH, 1);
        date = cl.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(fmt);
        returnDate = sdf.format(date);
        return returnDate;
    }

    /**
     * .
     * 获取一个月的最后一天
     *
     * @param fmt 格式
     * @return String
     */
    public static String getMonthLastDay(String fmt) {
        String returnDate;
        Date date;
        Calendar cl = Calendar.getInstance();
        switch (cl.get(Calendar.MONTH)) {
            case 0:
                cl.set(Calendar.DAY_OF_MONTH, 31);
                break;
            case 1:
                int year = cl.get(Calendar.YEAR);
                if (isLeapYear(year)) {
                    cl.set(Calendar.DAY_OF_MONTH, 29);
                } else {
                    cl.set(Calendar.DAY_OF_MONTH, 28);
                }
                break;
            case 2:
                cl.set(Calendar.DAY_OF_MONTH, 31);
                break;
            case 3:
                cl.set(Calendar.DAY_OF_MONTH, 30);
                break;
            case 4:
                cl.set(Calendar.DAY_OF_MONTH, 31);
                break;
            case 5:
                cl.set(Calendar.DAY_OF_MONTH, 30);
                break;
            case 6:
                cl.set(Calendar.DAY_OF_MONTH, 31);
                break;
            case 7:
                cl.set(Calendar.DAY_OF_MONTH, 31);
                break;
            case 8:
                cl.set(Calendar.DAY_OF_MONTH, 30);
                break;
            case 9:
                cl.set(Calendar.DAY_OF_MONTH, 31);
                break;
            case 10:
                cl.set(Calendar.DAY_OF_MONTH, 30);
                break;
            case 11:
                cl.set(Calendar.DAY_OF_MONTH, 31);
                break;
            default:
                cl.set(Calendar.DAY_OF_MONTH, 30);
        }
        date = cl.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(fmt);
        returnDate = sdf.format(date);
        return returnDate;
    }

    /**
     * .
     * 获取一个月的最后一天
     *
     * @param nowDate   输入时间
     * @param inFormat  输入格式
     * @param outFormat 输出格式
     * @return String
     */
    public static String getLastDay(String nowDate, String inFormat, String outFormat) {

        String returnDate = null;

        Date date;
        SimpleDateFormat sdf = new SimpleDateFormat(inFormat);
        try {
            date = sdf.parse(nowDate);

            Calendar cl = Calendar.getInstance();
            cl.setTime(date);
            switch (cl.get(Calendar.MONTH)) {
                case 0:
                    cl.set(Calendar.DAY_OF_MONTH, 31);
                    break;
                case 1:

                    int year = cl.get(Calendar.YEAR);

                    if (isLeapYear(year)) {
                        cl.set(Calendar.DAY_OF_MONTH, 29);
                    } else {
                        cl.set(Calendar.DAY_OF_MONTH, 28);
                    }
                    break;
                case 2:
                    cl.set(Calendar.DAY_OF_MONTH, 31);
                    break;
                case 3:
                    cl.set(Calendar.DAY_OF_MONTH, 30);
                    break;
                case 4:
                    cl.set(Calendar.DAY_OF_MONTH, 31);
                    break;
                case 5:
                    cl.set(Calendar.DAY_OF_MONTH, 30);
                    break;
                case 6:
                    cl.set(Calendar.DAY_OF_MONTH, 31);
                    break;
                case 7:
                    cl.set(Calendar.DAY_OF_MONTH, 31);
                    break;
                case 8:
                    cl.set(Calendar.DAY_OF_MONTH, 30);
                    break;
                case 9:
                    cl.set(Calendar.DAY_OF_MONTH, 31);
                    break;
                case 10:
                    cl.set(Calendar.DAY_OF_MONTH, 30);
                    break;
                case 11:
                    cl.set(Calendar.DAY_OF_MONTH, 31);
                    break;
                default:
                    cl.set(Calendar.DAY_OF_MONTH, 30);
            }
            date = cl.getTime();
            sdf = new SimpleDateFormat(outFormat);
            returnDate = sdf.format(date);
        } catch (ParseException e) {
            log.debug("获取该月最后一天失败", e);
        }
        return returnDate;
    }

    /**
     * .
     * 详细设计： 1.被400整除是闰年，否则： 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
     * 3.能被4整除同时能被100整除则不是闰年
     *
     * @param year 年
     * @return boolean
     */
    public static boolean isLeapYear(int year) {
        if ((year % 400) == 0) {
            return true;
        } else if ((year % 4) == 0) {
            return (year % 100) != 0;
        } else {
            return false;
        }
    }

    /**
     * .
     * 判断当前时间是否在时间区间内
     *
     * @param begindate 开始时间
     * @param enddate   结束时间
     * @param format    格式
     * @return boolean
     */
    public static boolean checkBetweenDate(String begindate, String enddate, String format) {
        boolean flag = false;
        Calendar cd = Calendar.getInstance();
        Date nowDate = cd.getTime();
        DateFormat df = new SimpleDateFormat(format);
        try {
            flag = nowDate.after(df.parse(begindate));
            if (flag) {
                flag = nowDate.before(df.parse(enddate));
            }
        } catch (ParseException e) {
            log.debug("", e);

        }
        return !flag;
    }

    /**
     * 将日期字符串转成Date类型
     *
     * @param str 格式:yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static Date strToDate(String str) {
        String[] dateTime = str.split(" ");
        String[] date = dateTime[0].split("-");
        String[] time = dateTime[1].split(":");

        int year = Integer.parseInt(date[0]);
        int month = Integer.parseInt(date[1]) - 1;
        int day = Integer.parseInt(date[2]);
        int hour = Integer.parseInt(time[0]);
        int minute = Integer.parseInt(time[1]);
        int second = Integer.parseInt(time[2]);

        Calendar cal = Calendar.getInstance();
        cal.set(year, month, day, hour, minute, second);

        return cal.getTime();
    }

    /**
     * 将日期字符串转成日期类型
     *
     * @return
     */
    public static Date strToDate(String str, String pattern) {
        try {
            SimpleDateFormat date = new SimpleDateFormat(pattern);
            return date.parse(str);
        } catch (ParseException e) {
            return null;
        }
    }


    /**
     * 获得前n个月份，包括当前月份
     *
     * @param number 月份数
     * @return 返回月份格式：yyyyMM，如：200810
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static List getBeforeMonths(int number) {
        List list = new ArrayList();

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 1);

        for (int i = 0; i < number; i++) {
            String year = String.valueOf(cal.get(Calendar.YEAR));
            String month = String.valueOf(cal.get(Calendar.MONTH) >= 10 ? (String.valueOf(cal.get(Calendar.MONTH))) : ("0" + cal.get(Calendar.MONTH)));
            if (cal.get(Calendar.MONTH) == 0) {
                year = String.valueOf(cal.get(Calendar.YEAR) - 1);
                month = "12";
            }

            list.add(year + month);

            cal.add(Calendar.MONTH, -1);
        }

        return list;
    }

    /**
     * 将指定格式的日期字符串(yyyyMMddHH24miss)解析为指定格式的日期(时间)
     *
     * @param pattern
     * @param source
     */
    public static String parse(String pattern, String source) {
        int year = Integer.parseInt(source.substring(0, 4));
        int month = Integer.parseInt(source.substring(4, 6)) - 1;
        int day = Integer.parseInt(source.substring(6, 8));
        int hour = Integer.parseInt(source.substring(8, 10));
        int minute = Integer.parseInt(source.substring(10, 12));
        int second = Integer.parseInt(source.substring(12));

        Calendar cal = Calendar.getInstance();
        cal.set(year, month, day, hour, minute, second);

        SimpleDateFormat date = new SimpleDateFormat(pattern);

        return date.format(cal.getTime());
    }

    /**
     * 获得前n个月份
     *
     * @param number 月份数
     * @return 返回月份格式：yyyyMM，如：200810
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static List<String> getLastMonths(int number) {
        List<String> list = new ArrayList();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
        for (int i = 0; i < number; i++) {
            String year = String.valueOf(cal.get(Calendar.YEAR));
            String month = String.valueOf(cal.get(Calendar.MONTH) >= 10 ? (String.valueOf(cal.get(Calendar.MONTH))) : ("0" + cal.get(Calendar.MONTH)));
            if (cal.get(Calendar.MONTH) == 0) {
                year = String.valueOf(cal.get(Calendar.YEAR) - 1);
                month = "12";
            }

            list.add(year + month);

            cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
        }

        return list;
    }

    /**
     * 格式化月份
     *
     * @param pattern
     * @param monthStyle 格式：yyyyMM，如：200810
     * @return
     */
    public static String formatOfMonth(String pattern, String monthStyle) {
        if (StringUtils.isEmpty(monthStyle)) {
            return "";
        }
        int year = Integer.parseInt(monthStyle.substring(0, 4));
        int month = Integer.parseInt(monthStyle.substring(4, 6)) - 1;

        Calendar cal = Calendar.getInstance();
        cal.set(year, month, 1);
        DateFormat date = new SimpleDateFormat(pattern);
        return date.format(cal.getTime());
    }

    /**
     * .
     * 检查日期的有效性
     *
     * @param date 格式：yyyyMMdd
     * @return boolean
     */
    public static boolean isValidDate(String date) {
        boolean bResult = true;
        if ((date == null) || (date.trim().length() != 8)) {
            bResult = false;
        } else {
            try {
                int year = Integer.parseInt(date.substring(0, 4));
                int month = Integer.parseInt(date.substring(4, 6));
                int day = Integer.parseInt(date.substring(6));
                Calendar cal = Calendar.getInstance();
                cal.setLenient(false);
                // 允许严格检查日期格式
                cal.set(year, month - 1, day);
                cal.getTime();
            } catch (Exception e) {
                bResult = false;
            }
        }
        return bResult;
    }

    /**
     * 近月（n月前/后），n为正数为n月后，n为负数为n月前.
     * 格式yyyyMM
     *
     * @param num
     * @return
     */
    public static String nearMonth(int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, num);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        String ye = String.valueOf(year);
        String mo = String.valueOf(month);
        if (mo.length() == 1) {
            mo = "0" + mo;
        }
        return ye + mo;
    }

    /**
     * 取当前时间,格式为YYYYMMDDHHMMSS的日期字符串.
     * <p>
     *
     * @return String 当前日期字符串.
     * @see Date
     */
    public static String getFormatDate(Date date, String pattern) {
        SimpleDateFormat f = new SimpleDateFormat(pattern);
        return f.format(date);
    }

    /**
     * 某个时间点 加减 N个月
     *
     * @param format
     * @param n
     * @return
     */
    public static String getDateAdd(int n, String dateStr, String format) {
        if (StringUtils.isEmpty(format)) {
            format = FMT_DATE_TIME_DEFAULT;
        }
        SimpleDateFormat df = new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(strToDate(dateStr, format));
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + n);
        return df.format(calendar.getTime());
    }

    /**
     * 某个时间点 加减 几天
     *
     * @param format
     * @param n
     * @return
     */
    public static String getDateAddDay(int n, String dateStr, String format) {

        if (StringUtils.isEmpty(format)) {
            format = FMT_DATE_TIME_DEFAULT;
        }

        SimpleDateFormat df = new SimpleDateFormat(format);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(strToDate(dateStr, format));

        calendar.add(Calendar.DATE, n);
        return df.format(calendar.getTime());
    }

    /**
     * 某个时间点 加减 几小时
     *
     * @param format
     * @param n
     * @return
     */
    public static String getDateAddHour(int n, String dateStr, String format) {
        if (StringUtils.isEmpty(format)) {
            format = FMT_DATE_TIME_DEFAULT;
        }
        SimpleDateFormat df = new SimpleDateFormat(format);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(strToDate(dateStr, format));

        calendar.add(Calendar.HOUR, n);
        return df.format(calendar.getTime());
    }

    public static List<String> dateTimeList(String startTime, String endTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
        LocalDateTime startDateTime = LocalDateTime.parse(startTime, formatter);
        LocalDateTime endDateTime = LocalDateTime.parse(endTime, formatter);

        List<String> dateTimeList = new ArrayList<>(); // 存储时间集合

        while (startDateTime.isBefore(endDateTime)) {
            LocalDateTime endOfDay = startDateTime.toLocalDate().atTime(LocalTime.MAX).withNano(0); // 当天最大时刻
            LocalDateTime endDateTimeOfDay = endOfDay.isBefore(endDateTime) ? endOfDay : endDateTime; // 当天最大时刻和 endtime 的较小值
            dateTimeList.add(startDateTime.format(formatter) + "=" + endDateTimeOfDay.format(formatter)); // 添加当天时间段
            startDateTime = endDateTimeOfDay.plusSeconds(1); // 从当天最大时刻的下一秒开始
        }

        return dateTimeList;
    }

    public static Long toTimeStamp(String dateTimeString) throws ParseException {
        // 定义日期时间格式化器
        SimpleDateFormat formatter = new SimpleDateFormat(FMT_DATE_TIME_DEFAULT);
        // 将字符串解析为Date
        Date date = formatter.parse(dateTimeString);
        // 获取时间戳（毫秒数）
        long timestamp = date.getTime();
        return timestamp;
    }

    public static Long getNowTimeStamp() {
        // 获取当前时间戳（毫秒）
        long currentTimeMillis = System.currentTimeMillis();
        // 如果你想获取以秒为单位的时间戳，可以除以1000
        return currentTimeMillis / 1000;
    }
}

